//
//  AmbitusListViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/4/13.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "AmbitusListViewController.h"
#import "MaintainViewController.h"
#import "NewsDetailViewController.h"
#import "MaintainCell.h"

@interface AmbitusListViewController (){
    NSMutableArray *_dataArray;
}

@end

@implementation AmbitusListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.navView.frame.size.height-38, 60, 30)];
    [rightBtn setTitle:@"地图" forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"back_n.png"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(gotoMapList:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:rightBtn];

    [self refreshData];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    // Do any additional setup after loading the view from its nib.
}

- (void)gotoMapList:(id)sender{
    MaintainViewController *vc = [[MaintainViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:vc animated:YES completion:^{}];
}

- (void)getDataList{
    [self getSpeclalLineList];
}

#pragma mark -请求接口
- (void)getSpeclalLineList{//专线列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"dong_jing":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].longitude],@"bei_wei":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].latitude]}];
    NSLog(@"%@",parameters);
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@per/list",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  [SVProgressHUD dismiss];
                                  NSLog(@"responseObject=%@",result);
                                  //        if ([(NSArray *)responseObject count] < self.pageLimit) {
                                  self.canLoadMore = NO;
                                  //        }else{
                                  //            self.canLoadMore = YES;
                                  //        }
                                  //        if (self.page == 1) {
                                  [self.tableView setContentOffset:CGPointZero animated:NO];
                                  [_dataArray removeAllObjects];
                                  [_dataArray addObjectsFromArray:result];
                                  [self addItemsOnTop];
                                  //        }else{
                                  //            [_dataArray addObjectsFromArray:responseObject];
                                  //            [self addItemsOnBottom];
                                  //        }
                                  
                              }];
    
}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    
    MaintainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MaintainCell" owner:self options:nil];
        cell = (MaintainCell *)[array objectAtIndex:0];
    }
    [cell configUIWithData:_dataArray[indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NewsDetailViewController *vc = [[NewsDetailViewController alloc] initWithNibName:nil bundle:nil];
    vc.navStr = @"周边维修";
    vc.newsInfo = _dataArray[indexPath.row];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
